package masterplugin.commande;

import java.io.File;
import java.util.ArrayList;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

public abstract class AbstractCommand {
	private ArrayList<String> resultLog;
	private String name;
	private File selected;
	
	public AbstractCommand(String name) {
		super();
		resultLog = new ArrayList<String>();
		this.name = name;
		append("No result to display at this time. Run the command first");
	}
	
	public final void run(){
		clear();
		if(!selected.exists()){
			manageNone();
		}
		else if(!selected.isDirectory()){
			manageFile(selected);
		}
		else{
			manageDirectory(selected);
		}
		for(String s : resultLog){
			System.out.println(s);
		}
	}
	
	
	public String getName(){
		return this.name;
	}
	
	protected abstract void manageNone();
	
	protected abstract void manageFile(File f);

	protected abstract void manageDirectory(File f);

	/**
	 * Ajoute le parametre arg dans le log des Resultat
	 * @param arg
	 */
	protected void append(String arg){
		resultLog.add(arg);
	}
	
	public void selectionChanged(IAction action, ISelection selection) {
		try{
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			String path = Platform.getLocation().toString()+structuredSelection.getFirstElement().toString().substring(1);
			selected = new File(path);
		}
		catch(NullPointerException npe){
			
		}
		
	}
	
	public void run(IAction action) {
		this.run();
	}
	
	/**
	 * Vide le log des Resultat
	 */
	protected void clear(){
		resultLog.clear();
	}
	/**
	 * Renvoie le log des résultats
	 * @return log
	 */
	public ArrayList<String> getResultLog() {
		return resultLog;
	}


	
	
	 
}
